import {
  trackingNo_query,delivery_query
} from '../../../api/shipping'
const util = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    logisticsData:[],
    orderId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
     console.log(options)
    this.loadList(options.id)
    this.data.orderId = options.id
  },
  loadList(data){
    var _this = this
    trackingNo_query({ orderId: data, pageNum: 1, pageSize:10}).then(res=>{
      console.log(res)
      if(res.type == 'success'){
         _this.data.logisticsData = res.data.data
        // for (var i = 0; i < _this.data.logisticsData.length; i++) {
        //   if (i == 0) {
        //     _this.data.logisticsData[i].spec1Reapet = '0';
        //   } else {
        //     if (_this.data.logisticsData[i].trackingNo == _this.data.logisticsData[i - 1].trackingNo) {
        //       _this.data.logisticsData[i].spec1Reapet = '1';
        //     } else {
        //       _this.data.logisticsData[i].spec1Reapet = '0';
        //     }
        //   }
        // }
        console.log(_this.data.logisticsData)
        _this.data.logisticsData.forEach(function(item){
         item.createDate = util.datetimeFormat(item.createDate)
        })
        _this.setData({
          logisticsData: _this.data.logisticsData
        })
      }
    }).catch(error => {
      console.log(error)
    })
  },
  detailsClick(data) {
    var _this = this
    console.log(data)
    if (data.currentTarget.dataset.type == 0){
      if (!util.isNull(data.currentTarget.dataset.trackingno)) {
        delivery_query({ trackingNo: data.currentTarget.dataset.trackingno}).then(res => {
          console.log(res)
          if (res.type == 'success') {
            wx.navigateTo({
              url: '/pages/member/logistics/details?trackimgno=' + data.currentTarget.dataset.trackingno,
            })
          }
        }).catch(error => {
          console.log("error")
          console.log(error)
        })
        
      } else {
        wx.showToast({
                icon: "none",
          title: '暂无物流信息',
        })
      }
    }else{
      wx.navigateTo({
        url: '/pages/member/logistics/packageDetails?orderid=' + this.data.orderId + '&productid=' + data.currentTarget.dataset.productId,
      })
    }
    
  }
})